<?php
HTML_Render_Head($js_vars,getTxt('AddSourceButton'));
echo $CSS_Main;
echo $JS_JQuery;
echo $CSS_JQX;
echo $JS_JQX;
echo $JS_GetTheme;
?>
<script type="text/javascript">

$(document).ready(function(){
	$("#msg").hide();
});
</script>
<?php HTML_Render_Body_Start(); 
genHeading('AddNewSource',true);
$attributes = array('class' => 'form-horizontal', 'name' => 'addsource', 'id' => 'addsource');
echo form_open('source/add', $attributes);
genInputT('Organization','Organization', 'Organization',true,$extra="",'ExTitle1');
//echo '<span class="em">' .getTxt('ExTitle1').'</span>';
genInputT('Description','SourceDescription','SourceDescription',true,$extra="",'ExDescript');
//echo '<span class="em">' .getTxt('ExDescript').'</span>';
genInputT('Link','SourceLink','SourceLink',false,$extra="",'ExMetaLink');
//echo '<span class="em">' .getTxt('ExMetaLink').'</span>';
genInputT('ContactName','ContactName','ContactName',true,$extra="",'ExName');
//echo '<span class="em">' .getTxt('ExName').'</span>';
genInputT('Phone','Phone','Phone', true,$extra="",'ExPhone');
//echo '<span class="em">' .getTxt('ExPhone').'</span>';
genInputT('Email','Email','Email', true,$extra="",'ExEmail');
//echo '<span class="em">' .getTxt('ExEmail').'</span>';
genInput('Address','Address','Address', true);
genInput('City','City','City', true);
genSelect('State','State','State',$stateOptions,'SelectEllipsis',true);
genInput('Zip','ZipCode','ZipCode', true);
genInputT('Citation','Citation','Citation',false,$extra="",'ExCitation');
//echo '<span class="em">' .getTxt('ExCitation').'</span>';
genInputT('MetadataIDSemicolon','MetadataID','MetadataID', false,' disabled','MetadataAutoGenerated');
//echo '<span class="em">' .getTxt('MetadataAutoGenerated').'</span>';
genSelect('TopicCategory','TopicCategory','TopicCategory',$topicOptions,'SelectEllipsis', true);
genInputT('Title','Title','Title', true,$extra="",'ExTitle2');
//echo '<span class="em">' .getTxt('ExTitle2').'</span>';
genInputT('Abstract','Abstract','Abstract', true,$extra="",'ExAbstract1');
//echo '<span class="em">' .getTxt('ExAbstract1').'</span>';
genInputT('MetaLink','MetadataLink','MetadataLink',false,$extra="",'Optional');
//echo '<span class="em">' .getTxt('Optional').'</span>';
?>
<div class="col-md-5 col-md-offset-5">
       <input type="SUBMIT" name="submit" value="<?php echo getTxt('AddSourceButton');?>" class="button"/>
       <input type="reset" name="Reset" value="<?php echo getTxt('Cancel'); ?>" class="button" style="width: auto" />
</div>
</div>
<?php HTML_Render_Body_End(); ?>
<script>
$("#resetButton").click(function() {
	$("form")[0].reset();
	 $("html, body").animate({ scrollTop: 0 }, "slow");
  return false;
});

$("#addsource").submit(function(){

	//Validate all fields
	if(($("#Organization").val())==""){
		//alert("Please enter an organization for the source.");
		alert(<?php echo "'".getTxt('EnterOrganization')."'"; ?>);
		return false;
	}

	if(($("#SourceDescription").val())==""){
		//alert("Please enter a description for the source.");
		alert(<?php echo "'".getTxt('EnterDescription')."'"; ?>);
		return false;
	}

	if(($("#SourceLink").val())!=""){
		var regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/;
		if(!($("#SourceLink").val().match(regexp))){
			alert(<?php echo "'".getTxt('InvalidSourceLinkURL')."'"; ?>);
			return false;
		}
	}

	if(($("#ContactName").val())==""){
		//alert("Please enter a contact name for the source.");
		alert(<?php echo "'".getTxt('EnterContactName')."'"; ?>);
		return false;
	}

	if(($("#Phone").val())==""){
		//alert("Please enter a phone number for the contact person.");
		alert(<?php echo "'".getTxt('EnterPhoneNumber')."'"; ?>);
		return false;
	}

	//Phone Validation
	var regex = /^([+]*([0-9]{1})*[- .(]*([0-9]{3})*[- .)]*[0-9]{3}[- .]*[0-9]{4})+$/;
	if(!($("#Phone").val().match(regex))){
		//alert("Invalid phone number");
		alert(<?php echo "'".getTxt('InvalidPhoneNumber')."'"; ?>);
		return false;
	}

	if(($("#Email").val())==""){
		//alert("Please enter an email address for the source.");
		alert(<?php echo "'".getTxt('EnterEmailAddress')."'"; ?>);
		return false;
	}
	//Email validation
	var pattern= /^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/;

	if(!($("#Email").val().match(pattern))){
		alert(<?php echo "'".getTxt('InvalidEmailAddress')."'"; ?>);
		return false;
	}

	if(($("#Address").val())==""){
		alert(<?php echo "'".getTxt('EnterAddress')."'"; ?>);
		return false;
	}
	
	if(($("#City").val())==""){
		alert(<?php echo "'".getTxt('EnterCity')."'"; ?>);
		return false;
	}

	if(($("#State option:selected").val())==-1){
		alert(<?php echo "'".getTxt('SelectSourceState')."'"; ?>);
		return false;
	}

	if(($("#ZipCode").val())=="" && (($("#State option:selected").val())!="NULL")){
		alert(<?php echo "'".getTxt('EnterZipCode')."'"; ?>);
		return false;
	}

	if((!($("#ZipCode").val().match(/^\d{5}(-\d{4})?$/))) && (($("#State option:selected").val())!="NULL")){
		alert(<?php echo "'".getTxt('InvalidZipCode')."'"; ?>);
		return false;
	}

	//Validate MetadataID info
	if(($("#TopicCategory option:selected").val())==-1){
		alert(<?php echo "'".getTxt('SelectTopicCategory')."'"; ?>);
		return false;
	}

	if(($("#Title").val())==""){
		alert(<?php echo "'".getTxt('EnterMetadataTitle')."'"; ?>);
		return false;
	}

	if(($("#Abstract").val())==""){
		alert(<?php echo "'".getTxt('EnterMetadataAbstract')."'"; ?>);
		return false;
	}

	if(($("#MetadataLink").val())!=""){
		var regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/;
			if(!($("#Metadatalink").val().match(regexp))){
				alert(<?php echo "'".getTxt('InvalidURLMetadata')."'"; ?>);
				return false;
			}
			
	}
return true;		
	});

</script>

